package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.WxUserMapper;
import com.ktmall.ktmalluijava.pojo.WxUser;
import com.ktmall.ktmalluijava.service.WxUserService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/8 下午9:03
 */
@Service
public class WxUserServiceImpl implements WxUserService {
    @Resource
    private WxUserMapper wxUserMapper;

    @Override
    public Page<WxUser> getPage(String username, Integer userLevel, Integer pageNo, Integer pageSize) {
        Page<WxUser> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(wxUserMapper.count(username,userLevel));
        page.setPageNo(pageNo);
        int offsets = (page.getPageNo()-1)*page.getPageSize();
        page.setData(wxUserMapper.select(username,userLevel,page.getPageSize(),offsets));
        return page;
    }

    @Override
    public int add(WxUser wxUser) {
        //如果没有上传头像的话，就给一个默认的头像
        if(wxUser.getAvatar()==null){
            wxUser.setAvatar("https://zs-2005.oss-cn-hangzhou.aliyuncs.com/2025-05-06/1746531789847.png");
        }
        //如果没有设置性别，则设置成未知0
        if(wxUser.getGender()==null){
            wxUser.setGender(0);
        }
        return wxUserMapper.add(wxUser);
    }

    @Override
    public int update(WxUser wxUser) {
        return wxUserMapper.update(wxUser);
    }

    /**
     * 会员详情信息
     *
     * @param id
     * @return
     */
    @Override
    public WxUser detail(Integer id) {
        return wxUserMapper.detail(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delete(Integer... id) {
        if(id.length==1){
            return wxUserMapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
            count += wxUserMapper.delete(integer);
        }
        return count;
    }
}
